package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.ActivityCoupons;
import com.sc.nft.entity.ActivityCouponsUser;
import com.sc.nft.entity.UserCoupons;
import com.sc.nft.entity.dto.UserCouponsDTO;
import com.sc.nft.enums.UserCouponsStatusEnum;
import com.sc.nft.mapper.UserCouponsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author liGen
 * @version 1.0
 * @date 2023/12/5 4:27 下午
 */
@Slf4j
@Repository
public class UserCouponsDao extends ServiceImpl<UserCouponsMapper, UserCoupons> {

    /**
     * 获取优惠券活动下用户的优惠券列表
     *
     * @param activityCouponsId
     * @param userId
     * @return
     */
    public List<UserCoupons> getByUserIdAndActivityCouponsId(Long activityCouponsId, Long userId, UserCouponsStatusEnum statusEnum) {
        LambdaQueryWrapper<UserCoupons> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCoupons::getUserId, userId);
        wrapper.eq(UserCoupons::getActivityCouponsId, activityCouponsId);
        wrapper.eq(UserCoupons::getStatus, statusEnum);
        return baseMapper.selectList(wrapper);
    }

    /**
     * 获取用户优惠券
     *
     * @param userId
     * @return
     */
    public List<UserCoupons> getUserCoupons(Long userId) {
        LambdaQueryWrapper<UserCoupons> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCoupons::getUserId, userId);
        return baseMapper.selectList(wrapper);
    }

    public boolean updateStatus(Long id, UserCouponsStatusEnum status) {
        UpdateWrapper<UserCoupons> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("status",status);
        return update(new UserCoupons(),wrapper);
    }

    /**
     * 获取订单下使用的优惠券
     *
     * @param orderId
     * @return
     */
    public UserCoupons getByOrderId(Long orderId) {
        LambdaQueryWrapper<UserCoupons> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCoupons::getOrderId, orderId);
        return baseMapper.selectOne(wrapper);
    }

    /**
     * 获取用户购买商品的优惠券
     *
     * @param productId
     * @return
     */
    public List<UserCouponsDTO> getUserCanCoupons(Long userId, Long productId, BigDecimal orderPrice) {
        return baseMapper.getUserCanCoupons(userId, productId, orderPrice);
    }
}
